<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="jquery-ui-1.10.4.custom/css/base/jquery-ui-1.10.4.custom.min.css">
    <script src="jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
    <script src="jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js"></script>
    <script>
        $(function(){
            // 创建数组
            var nameArray=new Array();
            let comanyArray=new Array();
            $.ajax({
                url:"companyList.do",
                type:"post",
                dataType:"json",
                success:function(obj){
                    for (let x in obj) {
                        // 向数组中存值
                        nameArray.push(obj[x].id+"")  // 存公司id
                        comanyArray.push(obj[x]);  // 存公司对象
                    }
                }
            })

            $("#id").autocomplete({
                source: nameArray,delay:100,minLength:1,messages: {
                    noResults: '',
                    results: function(){
                    }
                },
                select:function (event,ui) {
                   let id=ui.item.value
                    for(let x in comanyArray){
                        if(id==comanyArray[x].id){
                            $("#name").val(comanyArray[x].name)
                        }
                    }
                }
            })
        })


        function add(){
            $.ajax({
            })
        }
    </script>
</head>
<body>
    <form>
        <input type="text" id="id" name="id" />
        <input type="text" id="name" name="name" />
        <input type="button" value="添加" onclick="add()">
    </form>
</body>
</html>
